import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyh_motorcade_app/constant/constant.dart';
import 'package:nyh_motorcade_app/entity/company_motorcade_card_entity.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';
import 'package:nyh_motorcade_app/utils/format_utils.dart';
import 'package:nyh_motorcade_app/widgets/custom_app_bar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:nyh_motorcade_app/widgets/custom_popup_menu_item.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/multi_state_widget.dart';
import 'package:nyh_motorcade_app/widgets/text_button_with_icon.dart';
import 'select_transfer_account_controller.dart';

class SelectTransferAccountPage extends StatelessWidget {
  final controller = Get.put(SelectTransferAccountController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colours.color_background,
      appBar: CustomAppBar(
        context: context,
        title: "选择账户",
        // actions: [
        //   Container(
        //     width: 220.w,
        //     margin: EdgeInsets.only(left: 5.w, top: 6.w, right: 16.w, bottom: 6.w),
        //     child: TextButtonWithIcon(
        //       onPressed: () {
        //         controller.goToSearchAccountPage();
        //       },
        //       icon: Image.asset("images/ic_search_gray.png"),
        //       label: Text(
        //         "请输入账户编号/账户名称",
        //         style: TextStyle(color: Colours.color_cfcfd5, fontSize: 14.sp),
        //         maxLines: 1,
        //         overflow: TextOverflow.ellipsis,
        //       ),
        //       textIconPadding: 10.w,
        //       style: ButtonStyle(
        //         backgroundColor: MaterialStateProperty.all(Colours.color_background),
        //         shape: MaterialStateProperty.all(
        //           RoundedRectangleBorder(
        //             borderRadius: BorderRadius.circular(28.w),
        //           ),
        //         ),
        //       ),
        //     ),
        //   ),
        // ],
      ),
      body: GetBuilder<SelectTransferAccountController>(
        builder: (controller) {
          return Column(
            children: [
              buildHeaderWidget(),
              buildListWidget(),
            ],
          );
        },
      ),
    );
  }

  Widget buildHeaderWidget() {
    return Container(
      padding: EdgeInsets.only(left: 8.w, right: 16.w),
      height: 40.w,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          TextButtonWithIcon(
            onPressed: () {
              showAccountTypeMenu();
            },
            icon: Image.asset(controller.showAccountTypeMenu ? "images/ic_arrow_up.png" : "images/ic_arrow_down.png"),
            label: Text(
              controller.accountTypeName,
              style: TextStyle(
                color: Colours.color_text_theme,
                fontSize: 14.sp,
              ),
            ),
            textIconPadding: 5.w,
            iconTextAlignment: IconTextAlignment.iconRightTextLeft,
          ),
          Text(
            "共${controller.companyMotorcadeCardList.length}条",
            style: TextStyle(
              color: Colours.color_b7b7c0,
              fontSize: 14.sp,
            ),
          )
        ],
      ),
    );
  }

  void showAccountTypeMenu() {
    if (controller.accountTypeList.isEmpty) {
      return;
    }
    controller.showAccountTypeMenu = true;
    controller.update();
    showMenu(
      context: Get.context!,
      position: RelativeRect.fromLTRB(16.w, ScreenUtil().statusBarHeight + 56 + 40.w, ScreenUtil().screenWidth, 0.w),
      items: controller.accountTypeList.asMap().keys.map((e) => CustomPopupMenuItem(e, controller.accountTypeList[e].name)).toList(),
      elevation: 5.w,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(10.w),
      ),
    ).then((value) {
      controller.setSelectAccountType(value);
    });
  }

  Widget buildListWidget() {
    return Expanded(
      child: MultiStateWidget(
        child: ListView.builder(
          shrinkWrap: true,
          physics: BouncingScrollPhysics(),
          itemCount: controller.companyMotorcadeCardList.length,
          itemBuilder: (context, index) {
            return buildItemWidget(index);
          },
        ),
        state: controller.multiState,
        onRetry: () {
          controller.onRetry();
        },
      ),
    );
  }

  Widget buildItemWidget(int index) {
    CompanyMotorcadeCardList entity = controller.companyMotorcadeCardList[index];
    return Container(
      margin: EdgeInsets.only(left: 16.w, right: 16.w, bottom: 12.w),
      decoration: BoxDecoration(
        color: Colours.color_white,
        borderRadius: BorderRadius.circular(10.w),
      ),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(10.w),
        child: Material(
          child: InkWell(
            onTap: () {
              Get.back(
                result: {
                  Constant.PARAM_ACCOUNT_ID: entity.id,
                  Constant.PARAM_ACCOUNT_NAME: entity.name,
                  Constant.PARAM_ACCOUNT_AMOUNT: entity.availableAmount,
                  Constant.PARAM_ACCOUNT_TYPE: entity.accountType,
                },
              );
            },
            child: Padding(
              padding: EdgeInsets.symmetric(vertical: 13.w, horizontal: 16.w),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    entity.name ?? "",
                    style: TextStyle(
                      color: Colours.color_text_theme,
                      fontSize: 16.sp,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  SizedBox(
                    height: 3.w,
                  ),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: [
                      Expanded(
                        child: Text(
                          entity.code ?? "",
                          style: TextStyle(
                            color: Colours.color_cfcfd5,
                            fontSize: 12.sp,
                          ),
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                      SizedBox(
                        width: 10.w,
                      ),
                      Text(
                        "余额 ${FormatUtils.formatMoney(entity.availableAmount, unit: "元")}",
                        style: TextStyle(
                          color: Colours.color_6f7082,
                          fontSize: 14.sp,
                        ),
                      ),
                    ],
                  )
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
